package com.jiutong.meipengdai.ui.activitys;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.IdentifyCodeRespond;
import com.jiutong.meipengdai.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipengdai.http.present.UserInfoPresent;
import com.jiutong.meipengdai.utils.TimerUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 通过短信获取密码
 * Created by suxi on 2017/6/2.
 */

public class PwdResetByMsgActivity extends BaseActivity implements RegisterAndLoginBiz {
    @Bind(R.id.rl_back)
    RelativeLayout rl_back;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_phonenum)
    TextView tv_phonenum;
    @Bind(R.id.tv_getcode)
    TextView tvGetcode;
    @Bind(R.id.et_identify_code)
    EditText et_identify_code;
    @Bind(R.id.btn_pwd_bymsg_next)
    Button btn_pwd_bymsg_next;
    @Bind(R.id.tv_repwd_verify)
    TextView tv_repwd_verify;
    private String phone;

    private UserInfoPresent userInfoPresent;

    //正在获取验证码
    private boolean isVrfCodeGetting;

    private String identifyCode;
    private String resCode;
    private TimerUtil timerUtil;
//    private boolean isTrue; //验证码是否正确

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_pwd_reset_bymsg;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText(R.string.reset_pwd);
        Bundle bundle = getIntent().getExtras();
        phone = bundle.getString("phone");
        tv_phonenum.setText(phone);

        btn_pwd_bymsg_next.setEnabled(false);
        et_identify_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(getVerificatCode().length()!=6){
                    btn_pwd_bymsg_next.setBackgroundResource(R.drawable.shape_light_gray_btn);
                    btn_pwd_bymsg_next.setEnabled(false);
                }else{
                    btn_pwd_bymsg_next.setBackgroundResource(R.drawable.selector_orange_btn_bg);
                    btn_pwd_bymsg_next.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if(getVerificatCode().length()!=6){
                    btn_pwd_bymsg_next.setBackgroundResource(R.drawable.shape_light_gray_btn);
                    btn_pwd_bymsg_next.setEnabled(false);
                }else{
                    btn_pwd_bymsg_next.setBackgroundResource(R.drawable.selector_orange_btn_bg);
                    btn_pwd_bymsg_next.setEnabled(true);
                }
            }
        });


        timerUtil = new TimerUtil(tvGetcode);
        timerUtil.setCountTimer();


//        timer = new CountDownTimer(60000, 1000) {
//
//            @Override
//            public void onTick(long millisUntilFinished) {
//                tvGetcode.setTextColor(Color.parseColor("#808080"));
//                tvGetcode.setText(millisUntilFinished / 1000 + "秒后重新获取");
//            }
//
//            @Override
//            public void onFinish() {
//                // TODO Auto-generated method stub
//                isVrfCodeGetting = false;
//                tvGetcode.setTextColor(Color.parseColor("#f09738"));
//                tvGetcode.setText("重新获取验证码");
//            }
//        };
        userInfoPresent = new UserInfoPresent(this, this);
    }

    @OnClick({R.id.rl_back, R.id.tv_getcode, R.id.btn_pwd_bymsg_next, R.id.tv_repwd_verify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_getcode:
                userInfoPresent.getAuthCode();
                break;
            case R.id.tv_repwd_verify:   //返回密码验证
                finish();
                break;
            case R.id.btn_pwd_bymsg_next:
                if (isRightAction()) {
                    Bundle bundle = new Bundle();
                    bundle.putString("identifyCode", identifyCode);
                    openActivity(ChangePwdActivity.class, bundle);
                    finish();
                }
                break;
        }
    }

    /**
     * 操作是否正确
     *
     * @return
     */
    private boolean isRightAction() {

        if (TextUtils.isEmpty(getVerificatCode())) {
            toastShortShow("请输入手机验证码");
            return false;
        }
        if (!identifyCode.equals(getVerificatCode())) {
            toastShortShow("您输入的验证码错误，请重新输入");
            return false;
        }
        return true;
    }

    @Override
    public String getPhoneNum() {
        return tv_phonenum.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getVerificatCode() {
        return et_identify_code.getText().toString().trim();
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public String getRelName() {
        return null;
    }

    @Override
    public String getIdCard() {
        return null;
    }

    @Override
    public String getEmail() {
        return null;
    }

    @Override
    public String getNewPhone() {
        return null;
    }

    @Override
    public String getCardNumber() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(IdentifyCodeRespond subjects) {
        if(subjects.getResCode().equals("200")){
            timerUtil.startTimer();
            identifyCode = subjects.getData();
        }else{
            toastShortShow(subjects.getResDesc());
        }
    }

    @Override
    public void onSuccess(Object object) {

    }

    @Override
    public void onError(Throwable e) {

    }
}
